using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Enum;
using Converters;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using SExperiment;
using SWellPlate;

namespace Report
{
	public class PatientReportCreator_Guoce : PatientReportCreator
	{
		public string KeyNegative = "$Negative";

		public string KeyPositive = "$Positive";

		private List<ChartData> _ChartDataList = new List<ChartData>();

		public List<ChartData> ChartDataList
		{
			get
			{
				return _ChartDataList;
			}
			set
			{
				_ChartDataList = value;
			}
		}

		protected override void UpdateCustom(WordprocessingDocument docx)
		{
			List<Table> list = new List<Table>();
			WordOperator.EnumerateTable(docx.MainDocumentPart.Document.Body.ChildElements, list);
			foreach (Table item in list)
			{
				UpdateTable(item);
			}
		}

		private void UpdatTableByKey(Table table, string key)
		{
			if (!(key == "样品名称"))
			{
				return;
			}
			List<TableRow> list = new List<TableRow>();
			int num = -1;
			for (int i = 0; i < table.ChildElements.Count; i++)
			{
				TableRow tableRow = table.ChildElements[i] as TableRow;
				if (tableRow != null)
				{
					num++;
					if (num != 0)
					{
						list.Add(tableRow);
					}
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			for (int num2 = ChartDataList.Count - list.Count; num2 > 0; num2--)
			{
				TableRow tableRow2 = list[0].Clone() as TableRow;
				table.Append(tableRow2);
				list.Add(tableRow2);
			}
			num = 0;
			for (int j = 0; j < ChartDataList.Count; j++)
			{
				ChartData chartData = ChartDataList[j];
				int num3 = 0;
				TableRow tableRow3 = list[num];
				for (int k = 0; k < tableRow3.ChildElements.Count; k++)
				{
					TableCell tableCell = tableRow3.ChildElements[k] as TableCell;
					if (tableCell == null)
					{
						continue;
					}
					num3++;
					string text = string.Empty;
					switch (num3)
					{
					case 1:
						text = chartData.WellCell.Well.Sample.SampleName;
						break;
					case 2:
						text = chartData.WellCell.Well.Sample.WellNames;
						break;
					case 3:
						text = chartData.WellCell.Well.Sample.Project.BasicInfo.Name;
						break;
					case 4:
						text = EnumManagement.GetSampleTypeName(chartData.SampleTargetItem.SampleType);
						break;
					case 5:
						if (chartData.SampleTargetItem.SampleType == SampleType.Standard)
						{
							double result = double.NaN;
							text = ((!double.TryParse(chartData.SampleTargetItem.Property, out result)) ? chartData.SampleTargetItem.Property : ResultFormat.Format(result, base.Experiment.ExperimentSetting.UseScientific ? "0.000E0" : "#,0.##"));
						}
						else
						{
							text = chartData.SampleTargetItem.Property;
						}
						break;
					case 6:
						text = chartData.SampleTargetItem.TargetName;
						break;
					case 7:
						text = ResultFormat.Format(chartData.SampleTargetItem.Result.CtValue, "0.00");
						break;
					case 8:
						text = ResultFormat.FormatCnValue(base.Experiment.CurrentSubset, chartData.WellCell.Well.Sample.Wells[0], chartData.SampleTargetItem.ChannelNo, chartData.SampleTargetItem.Result.CnValue, base.Experiment.ExperimentSetting.UseScientific ? "0.000E0" : "#,0.00");
						break;
					case 9:
						text = chartData.SampleTargetItem.Result.GetAllConclusion();
						break;
					}
					List<Text> list2 = new List<Text>();
					List<Drawing> drawingList = new List<Drawing>();
					WordOperator.EnumerateTextAndDrawing(tableCell.ChildElements, list2, drawingList);
					if (list2.Count == 0)
					{
						bool flag = false;
						foreach (OpenXmlElement childElement in tableCell.ChildElements)
						{
							if (childElement is Paragraph)
							{
								childElement.Append(new Run(new Text(text)));
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							tableCell.Append(new Paragraph(new Run(new Text(text))));
						}
					}
					else
					{
						list2[0].Text = text;
						for (int l = 1; l < list2.Count; l++)
						{
							list2[l].Text = string.Empty;
						}
					}
				}
				num++;
			}
		}

		private void UpdateTable(Table table)
		{
			bool flag = false;
			for (int i = 0; i < table.ChildElements.Count; i++)
			{
				if (flag)
				{
					break;
				}
				TableRow tableRow = table.ChildElements[i] as TableRow;
				if (tableRow == null)
				{
					continue;
				}
				for (int j = 0; j < tableRow.ChildElements.Count; j++)
				{
					if (flag)
					{
						break;
					}
					TableCell tableCell = tableRow.ChildElements[j] as TableCell;
					if (tableCell == null)
					{
						continue;
					}
					List<Text> list = new List<Text>();
					List<Drawing> drawingList = new List<Drawing>();
					WordOperator.EnumerateTextAndDrawing(tableCell.ChildElements, list, drawingList);
					StringBuilder stringBuilder = new StringBuilder();
					foreach (Text item in list)
					{
						stringBuilder.Append(item.Text);
					}
					if (stringBuilder.Length > 0)
					{
						UpdatTableByKey(table, stringBuilder.ToString());
					}
					flag = true;
					break;
				}
			}
		}

		protected override WordKey GetKey(string text)
		{
			WordKey wordKey = null;
			string text2 = null;
			if (text.IndexOf(KeyNegative) == 0)
			{
				text2 = KeyNegative;
			}
			else if (text.IndexOf(KeyPositive) == 0)
			{
				text2 = KeyPositive;
			}
			if (!string.IsNullOrEmpty(text2))
			{
				wordKey = new WordKey();
				wordKey.Key = text2;
				wordKey.Content = text;
			}
			else
			{
				wordKey = base.GetKey(text);
			}
			return wordKey;
		}

		protected override string GetKeyValue(WordKey wordKey)
		{
			string text = string.Empty;
			if (wordKey.Key == KeyNegative)
			{
				List<Sample> list = (from s in base.Experiment.Wells
					where s.Sample != null && s.Targets[0].SampleType == SampleType.Negative
					select s.Sample).Distinct().ToList();
				{
					foreach (Sample item in list)
					{
						string allConclusion = item.Result.GetAllConclusion();
						if (!text.Contains(allConclusion))
						{
							text += allConclusion;
						}
					}
					return text;
				}
			}
			if (wordKey.Key == KeyPositive)
			{
				List<Sample> list2 = (from s in base.Experiment.Wells
					where s.Sample != null && s.Targets[0].SampleType == SampleType.HighPositive
					select s.Sample).Distinct().ToList();
				{
					foreach (Sample item2 in list2)
					{
						string allConclusion2 = item2.Result.GetAllConclusion();
						if (!text.Contains(allConclusion2))
						{
							text += allConclusion2;
						}
					}
					return text;
				}
			}
			return base.GetKeyValue(wordKey);
		}
	}
}
